﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Text.RegularExpressions;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore;

namespace STHPMS1.BLL.B_Bus
{
    public class E_NetevaluBus : BllBase, IE_NetevaluBill
    {
        public void Save<T>(T entity)
            where T : class ,IE_Netevalu
        {
            this.Scene.Dal.Sthpms1.Save(entity);
        }

        public void Remove(IE_Netevalu entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IE_Netevalu
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }

        public double? GetLenght(string inspecid, string searchvalue)
        {
            return this.Scene.Dal.Sthpms1.E_Netevalu.GetLenght(inspecid, searchvalue);
        }

        public double? GetAvageMQI(string inspecid, string searchvalue)
        {
            return this.Scene.Dal.Sthpms1.E_Netevalu.GetAvageMQI(inspecid, searchvalue);
        }

      
        public void Save(string inspecid, string roadnetname)
        {

            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();

            this.Scene.Dal.Sthpms1.DeleteAll<E_Netevalu>();

            E_Netevalu info = new E_Netevalu();
            info.Id = serv.NewGuid();
            info.RoadnetName = roadnetname;
            //info.Admdiv = "";  //行政区域
            info.Evalyear = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M; //评定年份

            string strsearch = this.Scene.Bll.B_Roadnet.GetSearchSQL(roadnetname);
            info.Netmqi = this.Scene.Bll.E_Netevalu.GetAvageMQI(inspecid, strsearch);

            info.Evalu_Length = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch)/1000;

            info.Netgdlength_Mqi = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and MQI>=80") / 1000;
            info.Netmdlength_Mqi = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and MQI>=70 and MQI<80") / 1000;
            info.Netbdlength_Mqi = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and MQI<70") / 1000;

            info.Netgdlength_Pqi = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and PQI>=80") / 1000;
            info.Netmdlength_Pqi = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and PQI>=70 and PQI<80") / 1000;
            info.Netbdlength_Pqi = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and PQI<70") / 1000;

            info.Netgdlength_Sci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and SCI>=80") / 1000;
            info.Netmdlength_Sci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and SCI>=70 and SCI<80") / 1000;
            info.Netbdlength_Sci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and SCI<70") / 1000;

            info.Netgdlength_Tci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and TCI>=80") / 1000;
            info.Netmdlength_Tci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and TCI>=70 and TCI<80") / 1000;
            info.Netbdlength_Tci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and TCI<70") / 1000;

            info.Netgdlength_Bci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and BCI>=80") / 1000;
            info.Netmdlength_Bci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and BCI>=70 and BCI<80") / 1000;
            info.Netbdlength_Bci = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, strsearch + " and BCI<70") / 1000;

            //double SumLenght = this.Scene.Bll.E_Netevalu.GetLenght(inspecid, "");
            info.Netgdrdrate_Mqi = info.Netgdlength_Mqi / info.Evalu_Length * 100;
            info.Netmdrdrate_Mqi = info.Netmdlength_Mqi / info.Evalu_Length * 100;
            info.Netbardrate_Mqi = info.Netbdlength_Mqi / info.Evalu_Length * 100;

            info.Netgdrdrate_Pqi = info.Netgdlength_Pqi / info.Evalu_Length * 100;
            info.Netmdrdrate_Pqi = info.Netmdlength_Pqi / info.Evalu_Length * 100;
            info.Netbardrate_Pqi = info.Netbdlength_Pqi / info.Evalu_Length * 100;

            info.Netgdrdrate_Sci = info.Netgdlength_Sci / info.Evalu_Length * 100;
            info.Netmdrdrate_Sci = info.Netmdlength_Sci / info.Evalu_Length * 100;
            info.Netbardrate_Sci = info.Netbdlength_Sci / info.Evalu_Length * 100;

            info.Netgdrdrate_Tci = info.Netgdlength_Tci / info.Evalu_Length * 100;
            info.Netmdrdrate_Tci = info.Netmdlength_Tci / info.Evalu_Length * 100;
            info.Netbardrate_Tci = info.Netbdlength_Tci / info.Evalu_Length * 100;

            info.Netgdrdrate_Bci = info.Netgdlength_Bci / info.Evalu_Length * 100;
            info.Netmdrdrate_Bci = info.Netmdlength_Bci / info.Evalu_Length * 100;
            info.Netbardrate_Bci = info.Netbdlength_Bci / info.Evalu_Length * 100;

            //Remove(info);
            //Save(info);
            this.Scene.Dal.Sthpms1.Insert<E_Netevalu>(info);
        }



        /// <summary>
        /// 待删除
        /// </summary>
        /// <param name="roadnetname"></param>
        /// <param name="year"></param>
        //public void Insert(string roadnetname, string year)
        //{
        //    E_Netevalu info = new E_Netevalu();
        //    info.RoadnetName = roadnetname;
        //    //info.Admdiv = "";
        //    //info.Evalyear = "";
        //    info.Netmqi = this.Scene.Bll.E_Evalu.GetAvageMQI(roadnetname, year);
        //    info.Evalu_Length = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year);

        //    info.Netgdlength_Mqi = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_MQI>=80");
        //    info.Netmdlength_Mqi = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_MQI>=70 and AVAGE_MQI<80");
        //    info.Netbdlength_Mqi = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_MQI<70");

        //    info.Netgdlength_Pqi = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_PQI>=80");
        //    info.Netmdlength_Pqi = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_PQI>=70 and AVAGE_PQI<80");
        //    info.Netbdlength_Pqi = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_PQI<70");

        //    info.Netgdlength_Sci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_SCI>=80");
        //    info.Netmdlength_Sci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_SCI>=70 and AVAGE_SCI<80");
        //    info.Netbdlength_Sci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_SCI<70");

        //    info.Netgdlength_Tci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_TCI>=80");
        //    info.Netmdlength_Tci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_TCI>=70 and AVAGE_TCI<80");
        //    info.Netbdlength_Tci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_TCI<70");

        //    info.Netgdlength_Bci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_BCI>=80");
        //    info.Netmdlength_Bci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_BCI>=70 and AVAGE_BCI<80");
        //    info.Netbdlength_Bci = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, " and AVAGE_BCI<70");

        //    double? SumLenght = this.Scene.Bll.E_Evalu.GetLenght(roadnetname, year, "");
        //    info.Netgdrdrate_Mqi = info.Netgdlength_Mqi / SumLenght;
        //    info.Netmdrdrate_Mqi = info.Netmdlength_Mqi / SumLenght;
        //    info.Netbardrate_Mqi = info.Netbdlength_Mqi / SumLenght;

        //    info.Netgdrdrate_Pqi = info.Netgdlength_Pqi / SumLenght;
        //    info.Netmdrdrate_Pqi = info.Netmdlength_Pqi / SumLenght;
        //    info.Netbardrate_Pqi = info.Netbdlength_Pqi / SumLenght;

        //    info.Netgdrdrate_Sci = info.Netgdlength_Sci / SumLenght;
        //    info.Netmdrdrate_Sci = info.Netmdlength_Sci / SumLenght;
        //    info.Netbardrate_Sci = info.Netbdlength_Sci / SumLenght;

        //    info.Netgdrdrate_Tci = info.Netgdlength_Tci / SumLenght;
        //    info.Netmdrdrate_Tci = info.Netmdlength_Tci / SumLenght;
        //    info.Netbardrate_Tci = info.Netbdlength_Tci / SumLenght;

        //    info.Netgdrdrate_Bci = info.Netgdlength_Bci / SumLenght;
        //    info.Netmdrdrate_Bci = info.Netmdlength_Bci / SumLenght;
        //    info.Netbardrate_Bci = info.Netbdlength_Bci / SumLenght;

        //    Remove(info);
        //    Save(info);

        //}
    }
}
